#import "JokeColumnView.h"
#import "JokeColumnCell.h"
#import "JokeModel.h"
#import "DBManager.h"
#import "NetWorkManager.h"

@implementation JokeColumnView

@synthesize owner,sortDislaying,sortPlaying;

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        //
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if(self){
        CGRect realFrame = self.frame;
        realFrame.size.height = WINDOW_HEIGHT;
        self.frame = realFrame;
        tipTimer = [[NSTimer scheduledTimerWithTimeInterval     :   0.3
                                                     target     :   self
                                                     selector   :   @selector(changeTipText:)
                                                     userInfo   :   nil
                                                      repeats   :   YES ] retain];
        
        [tipTimer setFireDate:[NSDate distantFuture]];
    }
    return self;
}

- (void)dealloc {
    PP_RELEASE(backBtn);
    PP_RELEASE(newBtn);
    PP_RELEASE(indicator);
    PP_RELEASE(leftBtn);
    PP_RELEASE(rightBtn);
    PP_RELEASE(tipText);
    PP_RELEASE(tipTextContainer);
    PP_RELEASE(tipTimer);
    PP_RELEASE(indexLabel);
    PP_RELEASE(topBar);
    PP_RELEASE(bottomBar);
    PP_RELEASE(allBtn);
    PP_RELEASE(hotBtn);
    PP_RELEASE(shitBtn);
    PP_RELEASE(allLine);
    PP_RELEASE(hotLine);
    PP_RELEASE(shitLine);
    PP_RELEASE(mainTableContainer);
    PP_RELEASE(mainTable);
    PP_RELEASE(egoHeader);
    PP_RELEASE(egoFooter);
    PP_RELEASE(sortMap);
    [super dealloc];
}

- (void)start:(BaseUIViewController<JokeColumnViewDelegate> *)pOwner :(JokeManager *)pJokeManagerAll :(JokeManager *)pJokeManagerHot :(JokeManager *)pJokeManagerShit {
    owner = pOwner;
    
    sortMap = [[NSMutableDictionary alloc] init];
    [sortMap setObject:[[[JCVSort alloc] init:self :nil :pJokeManagerAll :allBtn :allLine] autorelease] forKey:[NSString stringWithFormat:@"%d",pJokeManagerAll.sortIndex]];
    [sortMap setObject:[[[JCVSort alloc] init:self :nil :pJokeManagerHot :hotBtn :hotLine] autorelease] forKey:[NSString stringWithFormat:@"%d",pJokeManagerHot.sortIndex]];
    [sortMap setObject:[[[JCVSort alloc] init:self :nil :pJokeManagerShit :shitBtn :shitLine] autorelease] forKey:[NSString stringWithFormat:@"%d",pJokeManagerShit.sortIndex]];
    sortPlaying = [sortMap objectForKey:[NSString stringWithFormat:@"%d",pJokeManagerAll.sortIndex]];
    [self setSortDisplay:allBtn];
    
    CGRect tipFrame = tipTextContainer.frame;
    tipFrame.origin.y =  47 + (mainTableContainer.frame.origin.y-47)/2-tipFrame.size.height/2;
    tipTextContainer.frame = tipFrame;
    
    mainTable = [[EasyTableView alloc] initWithFrame:CGRectMake(0, 0, mainTableContainer.frame.size.width, mainTableContainer.frame.size.height) numberOfColumns:1 ofWidth:220];
    mainTable.delegate					= self;
    mainTable.tableView.allowsSelection	= YES;
    mainTable.tableView.scrollsToTop    = NO;
    mainTable.tableView.separatorColor		= [UIColor clearColor];
    mainTable.tableView.backgroundColor     = [UIColor clearColor];
    mainTable.cellBackgroundColor			= [UIColor clearColor];
    mainTable.autoresizingMask				= UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
    [mainTableContainer addSubview:mainTable];
    
    if (egoHeader == nil) {
        EGORefreshTableHeaderView *egoHeaderView = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - mainTable.tableView.bounds.size.height + 45, self.frame.size.width, mainTable.tableView.bounds.size.height)];
        egoHeaderView.delegate = self;
        [mainTable.tableView insertSubview:egoHeaderView atIndex:0];
        egoHeader = egoHeaderView;
    }
    [egoHeader refreshLastUpdatedDate];
    
    indicator.animationDuration = 0.4;
    indicator.animationImages = [NSArray arrayWithObjects:
                                 [UIImage imageNamed:@"fan_1.png"],
                                 [UIImage imageNamed:@"fan_2.png"],
                                 [UIImage imageNamed:@"fan_3.png"],
                                 [UIImage imageNamed:@"fan_4.png"],
                                 [UIImage imageNamed:@"fan_5.png"],
                                 [UIImage imageNamed:@"fan_6.png"],
                                 [UIImage imageNamed:@"fan_7.png"],
                                 [UIImage imageNamed:@"fan_8.png"],
                                 nil];
    
    if (egoFooter == nil) {
        EGORefreshTableFooterView *egoFooterView = [[EGORefreshTableFooterView alloc] initWithFrame:CGRectMake(0.0f, mainTable.tableView.contentSize.width, self.frame.size.width, mainTable.tableView.bounds.size.height)];
        egoFooterView.delegate = self;
        [mainTable.tableView insertSubview:egoFooterView atIndex:0];
        egoFooter = egoFooterView;
    }
    [egoFooter refreshLastUpdatedDate];
    egoFooter.hidden = YES;
    
    [owner.navigationController setNavigationBarHidden:YES animated:NO];
    CGAffineTransform currentTransform = mainTable.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, 2.0f, 2.0f);
    mainTable.transform = newTransform;
    CGRect tableFrame = mainTable.frame;
    tableFrame.origin.y = 50;
    mainTable.frame = tableFrame;
    self.superview.alpha = 0;
    CGAffineTransform topTransform = topBar.transform;
    CGAffineTransform newTopTransform = CGAffineTransformScale(topTransform, 2.0f, 2.0f);
    topBar.transform = newTopTransform;
    CGRect topFrame = topBar.frame;
    topFrame.origin.y = topFrame.origin.y + 20;
    topBar.frame = topFrame;
    CGAffineTransform bottomTransform = bottomBar.transform;
    CGAffineTransform newBottomTransform = CGAffineTransformScale(bottomTransform, 2.0f, 2.0f);
    bottomBar.transform = newBottomTransform;
    CGRect bottomFrame = bottomBar.frame;
    bottomFrame.origin.y = bottomFrame.origin.y + 100;
    bottomBar.frame = bottomFrame;
    self.superview.alpha = 0;
    self.superview.userInteractionEnabled = NO;
    [owner.navigationController setNavigationBarHidden:NO animated:YES];
    
    [self showLoadingTip];
}

#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods

- (void)egoRefreshTableHeaderDidStart:(EGORefreshTableHeaderView*)view :(int)rolling {
    if( sortPlaying.jokeManager.gettingNext || sortPlaying.jokeManager.gettingNew ){
        return;
    }
    indicator.image = [UIImage imageNamed:[NSString stringWithFormat:@"fan_%d.png",rolling]];
    newBtn.alpha = 0;
    indicator.alpha = 1;
}

- (void)egoRefreshTableHeaderDidStop:(EGORefreshTableHeaderView*)view {
    if( sortPlaying.jokeManager.gettingNext || sortPlaying.jokeManager.gettingNew ){
        return;
    }
    newBtn.alpha = 1;
    indicator.alpha = 0;
}

- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view {
	[self refreshNew:nil];
	[self performSelector:@selector(doneEgoHeaderLoading) withObject:nil afterDelay:1.0];
}

- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view {
    // should return if data source model is reloading
    //	return sortPlaying.jokeManager.gettingNew;
    return ( sortPlaying.jokeManager.gettingNext || sortPlaying.jokeManager.gettingNew );
}

- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view {
	// should return date data source was last changed
    return [NSDate date];
}

- (void)doneEgoHeaderLoading {
	//  model should call this when its done loading
	[egoHeader egoRefreshScrollViewDataSourceDidFinishedLoading:mainTable.tableView];
}

#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods

- (void)egoRefreshTableFooterDidStart:(EGORefreshTableFooterView*)view :(int)rolling {
    if( sortPlaying.jokeManager.gettingNext || sortPlaying.jokeManager.gettingNew ){
        return;
    }
    indicator.image = [UIImage imageNamed:[NSString stringWithFormat:@"fan_%d.png",rolling]];
    newBtn.alpha = 0;
    indicator.alpha = 1;
}

- (void)egoRefreshTableFooterDidStop:(EGORefreshTableFooterView*)view {
    if( sortPlaying.jokeManager.gettingNext || sortPlaying.jokeManager.gettingNew ){
        return;
    }
    newBtn.alpha = 1;
    indicator.alpha = 0;
}

- (void)egoRefreshTableFooterDidTriggerRefresh:(EGORefreshTableFooterView*)view{
	[self refreshNext:nil];
	[self performSelector:@selector(doneEgoFooterLoading) withObject:nil afterDelay:1.0];
}

- (BOOL)egoRefreshTableFooterDataSourceIsLoading:(EGORefreshTableFooterView*)view{
    // should return if data source model is reloading
    //	return sortPlaying.jokeManager.gettingNext;
    return ( sortPlaying.jokeManager.gettingNext || sortPlaying.jokeManager.gettingNew );
}

- (NSDate*)egoRefreshTableFooterDataSourceLastUpdated:(EGORefreshTableFooterView*)view{
	// should return date data source was last changed
    return [NSDate date];
}

- (void)doneEgoFooterLoading{
	//  model should call this when its done loading
	[egoFooter egoRefreshScrollViewDataSourceDidFinishedLoading:mainTable.tableView];
}

#pragma mark -
#pragma mark business Methods

- (void)reload:(JokeManager *)pJokeManager :(NSIndexPath *)pJokeIndexPath {
    if( pJokeIndexPath == nil ){
        return;
    }
    JCVSort *one = [sortMap objectForKey:[NSString stringWithFormat:@"%d",pJokeManager.sortIndex]];
    one.jokeIndexPath = pJokeIndexPath;
    if( one != sortDislaying ){
        return;
    }
    Log(@"JCV reload sort : %d, display joke : %d",one.jokeManager.sortIndex, one.jokeIndexPath.row);
    dispatch_async(dispatch_get_main_queue(), ^{
        CGPoint offset = mainTable.contentOffset;
        offset.x = 220 * pJokeIndexPath.row - 10;
        int right = mainTable.tableView.contentSize.height - mainTableContainer.frame.size.width;
        int left = 0;
        offset.x = offset.x > right ? right : offset.x;
        offset.x = offset.x < left ? left : offset.x;
        mainTable.contentOffset = offset;
        [self easyTableView:mainTable scrolledToOffset:offset];
    });
}

- (void)replace:(int)count {
    dispatch_async(dispatch_get_main_queue(), ^{
        CGPoint offset = mainTable.contentOffset;
        float newX = count > 0 ? offset.x + 220 * count : 0;
        int right = mainTable.tableView.contentSize.height - mainTableContainer.frame.size.width;
        int left = 0;
        offset.x = newX > right ? right : newX;
        offset.x = offset.x < left ? left : newX;
        mainTable.contentOffset = offset;
        [self easyTableView:mainTable scrolledToOffset:offset];
    });
}

- (void)replaceEgoFooter {
    dispatch_async(dispatch_get_main_queue(), ^{
        CGRect egoFooterFrame = egoFooter.frame;
        egoFooterFrame.origin.y = mainTable.tableView.contentSize.height-45;
        egoFooter.frame = egoFooterFrame;
        if( sortDislaying.jokeManager.listShow.count == 0){
            egoFooter.hidden = YES;
        } else {
            egoFooter.hidden = NO;
        }
    });
}

- (void)doneNew:(JokeManager *)pJokeManager :(int)newCount {
    Log(@"JCV doneNew : %d", newCount);
    if( pJokeManager.sortIndex != sortDislaying.jokeManager.sortIndex ){
        return;
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        [mainTable.tableView reloadData];
        if( newCount == 0 ){
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                sleep(1.0);
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self hideLoadingTip];
                });
            });
        }else{
            [self showAutoHideTip:[NSString stringWithFormat:@"发现最新%d条冷货",newCount] :newCount :0];
            [self replace:0];
        }
        [egoHeader egoRefreshScrollViewDataSourceDidFinishedLoading:mainTable.tableView];
        [self replaceEgoFooter];
    });
}

- (void)doneNext:(JokeManager *)pJokeManager :(int)nextCount {
    Log(@"JCV doneNext : %d", nextCount);
    if( pJokeManager.sortIndex != sortDislaying.jokeManager.sortIndex ){
        return;
    }
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        sleep(1.0);
        dispatch_async(dispatch_get_main_queue(), ^{
            [egoFooter egoRefreshScrollViewDataSourceDidFinishedLoading:mainTable.tableView];
            if( nextCount == 0 ){
                [self showAutoHideTip:[NSString stringWithFormat:@"正在加载，请稍后再试"] :0 :0];
            }else{
                [mainTable.tableView reloadData];
                [self replaceEgoFooter];
                [self showAutoHideTip:[NSString stringWithFormat:@"发现之前%d条冷货",nextCount] :0 :nextCount];
            }
        });
    });
}

- (void)doneNewAndNext:(JokeManager *)pJokeManager :(int)newCount :(int)nextCount {
    Log(@"JCV doneNewAndNext : %d %d", newCount, nextCount);
    if( pJokeManager.sortIndex != sortDislaying.jokeManager.sortIndex ){
        return;
    }
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        sleep(1.0);
        dispatch_async(dispatch_get_main_queue(), ^{
            [mainTable.tableView reloadData];
            if( newCount+nextCount == 0 ){
                [self showAutoHideTip:[NSString stringWithFormat:@"正在加载，请稍后再试"] :0 :0];
                return;
            }
            [self replaceEgoFooter];
            if( newCount>0 ){
                [self replace:0];
            }
            [self showAutoHideTip:[NSString stringWithFormat:@"发现%d条冷货",newCount+nextCount] :newCount :nextCount];
        });
    });
}

- (void)showLoadingTip {
    newBtn.alpha = 0;
    indicator.alpha = 0;
    [indicator startAnimating];
    leftBtn.alpha = 0.02f;
    rightBtn.alpha = 0.02f;
    tipText.text = @"";
    [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationCurveEaseOut animations:^(void){
        tipText.alpha =  1.0f;
        indicator.alpha = 1;
    }completion:^(BOOL bl){
        //定时器
        [tipTimer setFireDate:[NSDate distantPast]];
    }];
}

- (void)changeTipText:(NSTimer *)timer {
    static int count = 0;
    count ++;
    if(count>20){
        count = 0;
    }
    int tmp = count % 4 ;
    NSMutableString *tip = [NSMutableString stringWithFormat:@"正在制冷"];
    for( int i=0; i<=4; ++i){
        if( i < tmp ){
            [tip appendFormat:@"."];
        }else{
            [tip appendFormat:@" "];
        }
    }
    tipText.text = tip;
}

- (void)showAutoHideTip:(NSString *)tipStr :(int)newCount :(int)nextCount {
    [self hideLoadingTip];
    tipText.text = tipStr;
    [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationCurveEaseOut animations:^(void){
        tipText.alpha =  1.0f;
        if( newCount>0){
            leftBtn.alpha = 1.0f;
        }
        if( nextCount>0){
            rightBtn.alpha = 1.0f;
        }
    }completion:^(BOOL bl){
        [UIView animateWithDuration:0.5 delay:0.5 options:UIViewAnimationCurveEaseOut animations:^(void){
            tipText.alpha = 0.0f;
            leftBtn.alpha = 0.02f;
            rightBtn.alpha = 0.02f;
        }completion:^(BOOL bl){
            tipText.text = @"";
            tipText.alpha =  1.0f;
        }];
    }];
}

- (void)hideLoadingTip {
    if( sortPlaying.jokeManager.gettingNext || sortPlaying.jokeManager.gettingNew ){
        return;
    }
    [indicator stopAnimating];
    [tipTimer setFireDate:[NSDate distantFuture]];
    tipText.text = @"";
    newBtn.alpha = 1;
    indicator.alpha = 0;
}

- (void)zoomIn {
    if( sortDislaying != sortPlaying ){
        [sortDislaying JCVSstop];
        sortDislaying = sortPlaying;
        [sortDislaying JCVSstart];
    }
    [mainTable.tableView reloadData];
    [self replaceEgoFooter];
    [self reload:sortDislaying.jokeManager :sortDislaying.jokeIndexPath];
    dispatch_async(dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.3 delay:0.2 options:UIViewAnimationCurveEaseOut animations:^(void){
            CGAffineTransform tableTransform = mainTable.transform;
            CGAffineTransform newTableTransform = CGAffineTransformScale(tableTransform, 0.5f, 0.5f);
            mainTable.transform = newTableTransform;
            CGRect tableFrame = mainTable.frame;
            tableFrame.origin.y = 0;
            mainTable.frame = tableFrame;
            
            CGAffineTransform topTransform = topBar.transform;
            CGAffineTransform newTopTransform = CGAffineTransformScale(topTransform, 0.5f, 0.5f);
            topBar.transform = newTopTransform;
            CGRect topFrame = topBar.frame;
            topFrame.origin.y = topFrame.origin.y - 20;
            topBar.frame = topFrame;
            CGAffineTransform bottomTransform = bottomBar.transform;
            CGAffineTransform newBottomTransform = CGAffineTransformScale(bottomTransform, 0.5f, 0.5f);
            bottomBar.transform = newBottomTransform;
            CGRect bottomFrame = bottomBar.frame;
            bottomFrame.origin.y = bottomFrame.origin.y - 100;
            bottomBar.frame = bottomFrame;
        }completion:^(BOOL bl){
        }];
        [UIView animateWithDuration:0.4 delay:0.1 options:UIViewAnimationCurveEaseOut animations:^(void){
            self.superview.alpha = 1;
        }completion:^(BOOL bl){
        }];
        self.superview.userInteractionEnabled = YES;
    });
}

- (void)zoomOut {
    dispatch_async(dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.6 delay:0.0 options:UIViewAnimationCurveEaseIn animations:^(void){
            CGAffineTransform currentTransform = mainTable.transform;
            CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, 2.0f, 2.0f);
            mainTable.transform = newTransform;
            CGRect tableFrame = mainTable.frame;
            tableFrame.origin.y = 100;
            mainTable.frame = tableFrame;
            
            CGAffineTransform topTransform = topBar.transform;
            CGAffineTransform newTopTransform = CGAffineTransformScale(topTransform, 2.0f, 2.0f);
            topBar.transform = newTopTransform;
            CGRect topFrame = topBar.frame;
            topFrame.origin.y = topFrame.origin.y + 20;
            topBar.frame = topFrame;
            CGAffineTransform bottomTransform = bottomBar.transform;
            CGAffineTransform newBottomTransform = CGAffineTransformScale(bottomTransform, 2.0f, 2.0f);
            bottomBar.transform = newBottomTransform;
            CGRect bottomFrame = bottomBar.frame;
            bottomFrame.origin.y = bottomFrame.origin.y + 100;
            bottomBar.frame = bottomFrame;
        }completion:^(BOOL bl){
        }];
        [UIView animateWithDuration:0.6 delay:0.0 options:UIViewAnimationCurveEaseIn animations:^(void){
            self.superview.alpha = 0;
        }completion:^(BOOL bl){
        }];
        self.superview.userInteractionEnabled = NO;
    });
}

#pragma mark -
#pragma mark business Methods

- (void)setSortDisplay:(UIButton*)sender {
    int sortIndex = sender.tag;
    dispatch_async(dispatch_get_main_queue(), ^{
        if(sortDislaying!=nil){
            [sortDislaying JCVSstop];
        }
        JCVSort *newSort = [sortMap objectForKey:[NSString stringWithFormat:@"%d",sortIndex]];
        if( self.sortDislaying == newSort ){
            [self leftORright:nil];
        }else{
            self.sortDislaying = newSort;
            [mainTable.tableView reloadData];
            [self replaceEgoFooter];
            [self reload:sortDislaying.jokeManager :sortDislaying.jokeIndexPath];
        }
        [sortDislaying JCVSstart];
    });
}

- (IBAction)back:(id)sender {
    [owner JCVselect:sortPlaying.jokeManager :sortPlaying.jokeIndexPath];
}

- (IBAction)refreshNew:(id)sender {
    HAS_NO_NETWORK(owner);
    if( sortDislaying.jokeManager.gettingNew || sortPlaying.jokeManager.gettingNext ){
        return;
    }
    [self showLoadingTip];
    [owner JCVgetNew];
}

- (IBAction)refreshNext:(id)sender {
    HAS_NO_NETWORK(owner);
    if( sortDislaying.jokeManager.gettingNew || sortPlaying.jokeManager.gettingNext ){
        return;
    }
    [self showLoadingTip];
    [owner JCVgetNext];
}

- (IBAction)refreshNewAndNext:(id)sender {
    HAS_NO_NETWORK(owner);
    if( sortDislaying.jokeManager.gettingNew || sortPlaying.jokeManager.gettingNext ){
        return;
    }
    [self showLoadingTip];
    [owner JCVgetNewAndNext];
}

- (IBAction)leftORright:(id)sender {
    BOOL leftNow = mainTable.contentOffset.x < mainTable.tableView.contentSize.height/2;
    if( leftNow ){
        [self viewRight:nil];
    }else{
        [self viewLeft:nil];
    }
}

- (IBAction)viewLeft:(id)sender {
    [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationCurveEaseIn animations:^(void){
        CGPoint offset = mainTable.contentOffset;
        offset.x = 0;
        mainTable.contentOffset = offset;
    }completion:^(BOOL bl){
    }];
}

- (IBAction)viewRight:(id)sender {
    [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationCurveEaseIn animations:^(void){
        CGPoint offset = mainTable.contentOffset;
        offset.x = mainTable.tableView.contentSize.height - mainTableContainer.frame.size.width;
        mainTable.contentOffset = offset;
    }completion:^(BOOL bl){
    }];
}

#pragma mark -
#pragma mark EasyTableViewDelegate

- (NSUInteger)numberOfSectionsInEasyTableView:(EasyTableView*)easyTableView {
    return 1;
}

- (NSUInteger)numberOfCellsForEasyTableView:(EasyTableView *)view inSection:(NSInteger)section {
    if(sortDislaying==nil||sortDislaying.jokeManager==nil||sortDislaying.jokeManager.listShow==nil){
        return 0;
    }else{
//        Log(@"count : %d",[sortDislaying.jokeManager.listShow count]);
        return [sortDislaying.jokeManager.listShow count];
    }
}

// The height of the footer section view MUST be the same as your HORIZONTAL_TABLEVIEW_HEIGHT (horizontal EasyTableView only)
- (UIView *)easyTableView:(EasyTableView*)easyTableView viewForHeaderInSection:(NSInteger)section {
    UILabel *label = [[[UILabel alloc] init]autorelease];
	label.textAlignment = UITextAlignmentCenter;
	label.frame = CGRectMake(0, 0, 50, 275);
    label.backgroundColor = [UIColor clearColor];
    return label;
}

// The height of the footer section view MUST be the same as your HORIZONTAL_TABLEVIEW_HEIGHT (horizontal EasyTableView only)
- (UIView *)easyTableView:(EasyTableView*)easyTableView viewForFooterInSection:(NSInteger)section {
    UILabel *label = [[[UILabel alloc] init]autorelease];
	label.textAlignment = UITextAlignmentCenter;
	label.frame = CGRectMake(0, 0, 40, 275);
    label.backgroundColor = [UIColor clearColor];
    return label;
}

// These delegate methods support both example views - first delegate method creates the necessary views
- (UIView *)easyTableView:(EasyTableView *)easyTableView viewForRect:(CGRect)rect {
    NSArray *viewNibList = [[NSBundle mainBundle] loadNibNamed:@"JokeColumnCell" owner:self options:nil];
    UIView *view = [[viewNibList objectAtIndex:0] retain];
    return view;
}

// Second delegate populates the views with data from a data source
- (void)easyTableView:(EasyTableView *)easyTableView setDataForView:(UIView *)view forIndexPath:(NSIndexPath *)indexPath {
    CGPoint offset = mainTable.contentOffset;
    int tmp = offset.x / 220;
    int index;
    if( tmp > indexPath.row){
        index = tmp + 1;
//        Log(@"index : %d - %d - %d",tmp, indexPath.row, index);
        indexLabel.text = [NSString stringWithFormat:@"%d / %d",(sortDislaying.jokeManager.listShow.count==0?0:index),sortDislaying.jokeManager.listShow.count];
    } else if( tmp < indexPath.row - 1 ){
        index = indexPath.row;
//        Log(@"index : %d - %d - %d",tmp, indexPath.row, index);
        indexLabel.text = [NSString stringWithFormat:@"%d / %d",(sortDislaying.jokeManager.listShow.count==0?0:index),sortDislaying.jokeManager.listShow.count];
    } else if( tmp <= 0 ){
        indexLabel.text = [NSString stringWithFormat:@"%d / %d",(sortDislaying.jokeManager.listShow.count==0?0:1),sortDislaying.jokeManager.listShow.count];
    }
    JokeModel *tJoke = [sortDislaying.jokeManager.listShow objectAtIndex:indexPath.row];
    dispatch_async(dispatch_get_main_queue(), ^{
        [(JokeColumnCell *)view setData:tJoke :sortDislaying.jokeManager.dbSort :(sortPlaying==sortDislaying&&indexPath.row==sortPlaying.jokeIndexPath.row)];
    });
}

- (void)easyTableView:(EasyTableView *)easyTableView scrolledToOffset:(CGPoint)contentOffset {
    if( contentOffset.x <= 0 ){
        indexLabel.text = [NSString stringWithFormat:@"%d / %d",(sortDislaying.jokeManager.listShow.count==0?0:1),sortDislaying.jokeManager.listShow.count];
        return;
    }
//    Log(@"index : %f - %f - %f",contentOffset.x, mainTable.tableView.contentSize.height, mainTable.tableView.contentSize.height-220);
    if( contentOffset.x >= mainTable.tableView.contentSize.height-mainTableContainer.frame.size.width ){
        indexLabel.text = [NSString stringWithFormat:@"%d / %d",sortDislaying.jokeManager.listShow.count,sortDislaying.jokeManager.listShow.count];
        return;
    }
}

- (void)easyTableView:(EasyTableView *)easyTableView selectedView:(UIView *)selectedView atIndexPath:(NSIndexPath *)indexPath deselectedView:(UIView *)deselectedView {
    self.sortPlaying = sortDislaying;
    [self reload:sortPlaying.jokeManager :indexPath];
    [owner JCVselect:sortPlaying.jokeManager :sortPlaying.jokeIndexPath];
}

- (void)easyTableViewDidScroll:(UIScrollView *)scrollView {
	[egoHeader egoRefreshScrollViewDidScroll:scrollView];
	[egoFooter egoRefreshScrollViewDidScroll:scrollView];
}

- (void)easyTableViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
	[egoHeader egoRefreshScrollViewDidEndDragging:scrollView];
	[egoFooter egoRefreshScrollViewDidEndDragging:scrollView];
}

@end

@implementation JCVSort
@synthesize owner,sortBtn,sortLine,jokeIndexPath,jokeManager;
-(id)init:(JokeColumnView *)pOwner :(NSIndexPath *)pJokeIndexPath :(JokeManager *)pJokeManager :(UIButton *)pBtn :(UIImageView *)pLine {
    self = [super init];
    if (self) {
        self.owner = pOwner;
        self.jokeIndexPath = pJokeIndexPath;
        self.jokeManager = pJokeManager;
        self.sortBtn = pBtn;
        self.sortLine = pLine;
        sortBtn.tag = pJokeManager.sortIndex;
        [sortBtn addTarget:owner action:@selector(setSortDisplay:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}
-(void)dealloc {
    [jokeIndexPath release]; jokeIndexPath = nil;
    [super dealloc];
}
-(void)JCVSstart {
    sortBtn.selected = YES;
    sortLine.hidden = NO;
    [jokeManager getStartAsync:(![[NetWorkManager sharedInstance] hasWifiNetwork])];
    if( jokeManager.gettingNext || jokeManager.gettingNew ){
        [owner showLoadingTip];
    }else{
        [owner hideLoadingTip];
    }
}
-(void)JCVSstop {
    sortBtn.selected = NO;
    sortLine.hidden = YES;
}
@end
